﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using System.Web.Security;

namespace BitPortal.Admin.Media.Gallery.Module
{
    public partial class GalleryAddEdit : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaEdit");
                PermissionBLL.CheckRoleThrowException("MediaInsert");
                Models.Gallery g = new Models.Gallery()
                {
                    Name = txtName.Text,
                    Description = txtDescription.Text,
                    Direction = cbDirection.SelectedItem.Value,
                    SortOrder = Convert.ToInt32(txtSortOrder.Text),
                    AutoPlay = ckAutoplay.Checked,
                    IsEnabled = ckIsEnabled.Checked,
                    ThumbImage = ThumbImage.TextFieldURL.Text,
                    Meta = new Meta() { MetaTitle = txtMetaTitle.Text, MetaDescription = txtMetaDescription.Text, MetaKeyword = txtMetaKeyword.Text }
                };
                //Logs
                g.Logs.DatetimeCreated = g.Logs.DatetimeEdited = DateTime.Now;
                g.Logs.CreatingUser = g.Logs.EditingUser = (Membership.GetUser() == null) ? null : (Guid?)new Guid(Membership.GetUser().ProviderUserKey.ToString());

                if (BitPortal.Business.GalleryBLL.Insert(g) > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm gallery thành công!");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaEdit");

                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                int galleryID = Convert.ToInt32(txtID.Text);
                BitPortal.Models.Gallery g = bitEnt.Galleries.SingleOrDefault(ent => ent.GalleryID == galleryID);

                if (g != null)
                {
                    g.Name = txtName.Text;
                    g.Description = txtDescription.Text;
                    g.Direction = cbDirection.SelectedItem.Value;
                    g.SortOrder = Convert.ToInt32(txtSortOrder.Text);
                    g.AutoPlay = ckAutoplay.Checked;
                    g.IsEnabled = ckIsEnabled.Checked;
                    g.ThumbImage = ThumbImage.TextFieldURL.Text;
                    //
                    g.Logs.DatetimeEdited = DateTime.Now;
                    g.Logs.EditingUser = (Membership.GetUser() == null) ? null : (Guid?)new Guid(Membership.GetUser().ProviderUserKey.ToString());
                    //
                    g.Meta = new Meta() { MetaTitle = txtMetaTitle.Text, MetaDescription = txtMetaDescription.Text, MetaKeyword = txtMetaKeyword.Text };
                }
                if (bitEnt.SaveChanges() > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Sửa gallery thành công!");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        public void BindData(int? galleryID = null)
        {
            if (galleryID == null)
            {
                txtName.Text = txtDescription.Text = ThumbImage.TextFieldURL.Text = txtMetaDescription.Text = txtMetaKeyword.Text = txtMetaTitle.Text = "";
                txtID.Text = txtSortOrder.Text = "0";
                ckAutoplay.Checked = ckIsEnabled.Checked = true;
                btnUpdate.Hidden = true;
                btnInsert.Hidden = false;
                Window1.Show();
                return;
            }
            Models.Gallery g = Business.GalleryBLL.GetByID((int)galleryID);
            if (g != null)
            {
                txtID.Text = g.GalleryID.ToString();
                txtName.Text = g.Name;
                txtDescription.Text = g.Description;
                cbDirection.SelectedItem.Value = g.Direction;
                txtSortOrder.Text = g.SortOrder.ToString();
                ckAutoplay.Checked = g.AutoPlay;
                ckIsEnabled.Checked = g.IsEnabled;
                ThumbImage.TextFieldURL.Text = g.ThumbImage;

                txtMetaTitle.Text = g.Meta.MetaTitle;
                txtMetaDescription.Text = g.Meta.MetaDescription;
                txtMetaKeyword.Text = g.Meta.MetaKeyword;

                btnUpdate.Hidden = false;
                btnInsert.Hidden = true;
                Window1.Show();
            }
        }

    }
}